// packageJ/outArea/areaSelect/areaSelect.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList:[],
    page:1,
    total_page: 0,
    isLoadMore: true,
    price:'',
    levelName:'',
    pushNum:0,
    selfPrice:0,
    havePushNum:0,
    applyBackground:'',
    siteName:'',
    imageUrl:'',
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getApplydata();
  },
  goodsNav(event) {
    let id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/packageA/detail_v2/detail_v2?id=${id}`,
    })
  },
  getApplydata() {
    let query = {};
    let urlStr = app.getNetAddresss("plugin.area-dividend.api.upgrade.index");
    app._postNetWork({
      url: urlStr,
      data:query,
      success:  (resdata) => {
        let res = resdata.data;
        let data = res.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.totalPage = data.goods_list.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            info: data,
            goodsList: data?.goods_list?.data,
            levelName:data.level_name,
            price:data.price,
            pushNum:data.push_num,
            selfPrice:data.self_price,
            havePushNum:data.have_push_num,
            applyBackground:data.apply_background
          })
        } else {
          this.data.page = this.data.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let query = {
        page:this.data.page,
      };
      let urlStr = app.getNetAddresss("plugin.area-dividend.api.upgrade.index");
      app._postNetWork({
        url: urlStr,
        data: query,
        success: (resdata) => {
          let res = resdata.data;
          let list = res?.data.goods_list.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              goodsList : this.data.goodsList.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  onShareTimeline() {}
})